﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace A._2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int n = 0;
        DateTime taskBegin = DateTime.Now;
        public MainWindow()
        {
            InitializeComponent();
            textBox.Text = "192.168.1";
            textBox1.Text = "1";
            textBox2.Text = "100";
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            IPAddress ipstart;
            IPAddress ipend;
            try
            {
               if (IPAddress.TryParse(textBox.Text + textBox1.Text, out ipstart) && IPAddress.TryParse(textBox.Text + textBox2.Text, out ipend)&&Int32.Parse(textBox1.Text)<Int32.Parse(textBox2.Text))
                {
                    ipstart = IPAddress.Parse(textBox.Text + textBox1.Text);
                    label4.Visibility = Visibility.Hidden;
                }
                else {
                    label4.Visibility = Visibility.Visible;
                }
            }
            catch
            {
                try
                {
                    label4.Visibility = Visibility.Hidden;
                }
                catch
                {
                    return;
                }
            }
        }
        private void scan(Object ip)
        {
            DateTime begin = DateTime.Now;
            IPAddress ipAddress = (IPAddress)ip;
            string hostName;
            try
            {
                hostName = Dns.GetHostEntry(ipAddress).HostName;
            }
            catch
            {
                hostName = "(不在线)";
            }
            DateTime end = DateTime.Now;
            TimeSpan ts = end - begin;
            listBox.Dispatcher.Invoke(() => listBox.Items.Add("扫描地址：" + ipAddress.ToString() + " 扫描用时：" + ts.TotalMilliseconds + "毫秒 " + " 主机名称：" + hostName));
            n--;
            if (n == 1)
            {
                DateTime taskEnd = DateTime.Now;
                TimeSpan taskAll = taskEnd - taskBegin;
                MessageBox.Show("扫描完毕!总用时:" + taskAll.TotalMilliseconds + "毫秒");
            }

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            IPAddress ipstart;
            IPAddress ipend;
            listBox.Items.Clear();
            if (textBox.Text[textBox.Text.Length - 1] != '.')
            {
                textBox.Text = textBox.Text + ".";
            }
            try
            {
                n = int.Parse(textBox2.Text) - int.Parse(textBox1.Text) + 1;
            }
            catch
            {
                label4.Visibility = Visibility.Visible;
                return;
            }
            try
            {
                for (int i = int.Parse(textBox1.Text); i < int.Parse(textBox2.Text); i++)
                {
                    IPAddress ip = IPAddress.Parse(textBox.Text + i.ToString());
                    Console.WriteLine(ip.ToString());
                    Thread t = new Thread(scan);
                    t.Start(ip);
                }
            }
            catch
            {
                label4.Visibility = Visibility.Visible;
                return;
            }
           
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
